package com.mail.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.mapper.*;
import com.mail.model.db.*;
import com.mail.model.dto.ProductParamDto;
import com.mail.model.dto.QueryProductDto;
import lombok.val;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @author yan
 * @date 2022/06/21 14:57
 **/
@DubboService
public class PmsProductApiImpl implements PmsProductApi {
	@Autowired
	private PmsProductMapper pmsProductMapper;
	@Autowired
	private PmsMemberPriceMapper pmsMemberPriceMapper;
	@Autowired
	private CmsPrefrenceAreaProductRelationMapper cmsPrefrenceAreaProductRelationMapper;
	@Autowired
	private PmsProductAttributeValueMapper pmsProductAttributeValueMapper;
	@Autowired
	private PmsProductFullReductionMapper pmsProductFullReductionMapper;
	@Autowired
	private PmsProductLadderMapper pmsProductLadderMapper;
	@Autowired
	private PmsSkuStockMapper pmsSkuStockMapper;
	@Autowired
	private CmsSubjectProductRelationMapper cmsSubjectProductRelationMapper;
	@Autowired
	private PmsProductAttributeMapper productAttributeMapper;
	@Autowired
	private PmsProductAttributeCategoryMapper productAttributeCategoryMapper;


	// 存入商品
	@Override
	public void saveProduct(PmsProduct pmsProduct) {
		pmsProductMapper.insert(pmsProduct);
	}

	// 封装商品会员价格表
	@Override
	public void saveMemberPrice(PmsMemberPrice memberPrice) {
		if (memberPrice != null) {
			pmsMemberPriceMapper.insert(memberPrice);
		}
	}

	// 封装优选专区和产品关系表
	@Override
	public void savePrefAreaProRelation(CmsPrefrenceAreaProductRelation prefrenceAreaProductRelation) {
		if (prefrenceAreaProductRelation != null) {
			cmsPrefrenceAreaProductRelationMapper.insert(prefrenceAreaProductRelation);
		}
	}

	// 封装存储产品参数信息的表
	@Override
	public void saveProAttValue(PmsProductAttributeValue productAttributeValue) {
		if (productAttributeValue != null) {
			pmsProductAttributeValueMapper.insert(productAttributeValue);
		}
	}

	// 封装产品满减表(只针对同商品)
	@Override
	public void saveProFullReduction(PmsProductFullReduction productFullReduction) {
		if (productFullReduction != null) {
			pmsProductFullReductionMapper.insert(productFullReduction);
		}
	}

	// 封装产品阶梯价格表(只针对同商品)
	@Override
	public void saveProLadder(PmsProductLadder productLadder) {
		if (productLadder != null) {
			pmsProductLadderMapper.insert(productLadder);
		}
	}

	// 封装sku的库存
	@Override
	public void saveSkuStock(PmsSkuStock skuStock) {
		if (skuStock != null) {
			pmsSkuStockMapper.insert(skuStock);

		}
	}

	// 封装专题商品关系表
	@Override
	public void saveSubProRelation(CmsSubjectProductRelation subjectProductRelation) {
		if (subjectProductRelation != null) {
			cmsSubjectProductRelationMapper.insert(subjectProductRelation);
		}
	}

	// 往product_attribute_category表中存入对应属性数量和参数数量
	@Override
	public void saveCount(Long productAttributeCategoryId) {

		LambdaQueryWrapper<PmsProductAttribute> attributeWrapper = new LambdaQueryWrapper<>();
		attributeWrapper.eq(PmsProductAttribute::getProductAttributeCategoryId, productAttributeCategoryId);
		// 指定attribute
		attributeWrapper.eq(PmsProductAttribute::getType, 0);
		Integer attributeCount = productAttributeMapper.selectCount(attributeWrapper);
		LambdaQueryWrapper<PmsProductAttribute> paramWrapper = new LambdaQueryWrapper<>();
		paramWrapper.eq(PmsProductAttribute::getProductAttributeCategoryId, productAttributeCategoryId);
		// 指定param
		paramWrapper.eq(PmsProductAttribute::getType, 1);
		Integer paramCount = productAttributeMapper.selectCount(paramWrapper);

		LambdaQueryWrapper<PmsProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(PmsProductAttributeCategory::getId, productAttributeCategoryId);
		PmsProductAttributeCategory one = productAttributeCategoryMapper.selectOne(wrapper);
		if (one != null) {
			PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
			productAttributeCategory.setId(productAttributeCategoryId);
			productAttributeCategory.setAttributeCount(attributeCount + one.getAttributeCount());
			productAttributeCategory.setParamCount(paramCount + one.getParamCount());
			productAttributeCategoryMapper.updateById(productAttributeCategory);
		} else {
			PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
			productAttributeCategory.setId(productAttributeCategoryId);
			productAttributeCategory.setAttributeCount(attributeCount);
			productAttributeCategory.setParamCount(paramCount);
			productAttributeCategoryMapper.insert(productAttributeCategory);
		}
	}

	// 分页查询商品
	@Override
	public Page<PmsProduct> queryProductList(QueryProductDto queryProductDto) {
		Page<PmsProduct> pageInfo = new Page<>(queryProductDto.getPageNum(), queryProductDto.getPageSize());
		LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(queryProductDto.getBrandId() != null, PmsProduct::getBrandId, queryProductDto.getBrandId());
		wrapper.like(queryProductDto.getKeyword() != null, PmsProduct::getName, queryProductDto.getKeyword());
		wrapper.eq(queryProductDto.getProductCategoryId() != null, PmsProduct::getProductCategoryId, queryProductDto.getProductCategoryId());
		wrapper.eq(queryProductDto.getProductSn() != null, PmsProduct::getProductSn, queryProductDto.getProductSn());
		wrapper.eq(queryProductDto.getPublishStatus() != null, PmsProduct::getPublishStatus, queryProductDto.getPublishStatus());
		wrapper.eq(queryProductDto.getVerifyStatus() != null, PmsProduct::getVerifyStatus, queryProductDto.getVerifyStatus());
		// 倒序排列
		wrapper.orderByDesc(PmsProduct::getSort);
		Page<PmsProduct> page = pmsProductMapper.selectPage(pageInfo, wrapper);
		return page;
	}

	// 根据商品名称或货号模糊查询
	@Override
	public List<PmsProduct> queryProductByKeyword(String keyword) {
		LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(keyword != null, PmsProduct::getName, keyword);
		List<PmsProduct> pmsProductList = pmsProductMapper.selectList(wrapper);
		return pmsProductList;
	}

	// 批量修改删除状态
	@Override
	public void deleteStatus(Integer deleteStatus, List<Integer> ids) {
		for (Integer id : ids) {
			LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
			wrapper.in(PmsProduct::getId, Long.valueOf(id));
//            PmsProduct pmsProduct = new PmsProduct();
//            pmsProduct.setDeleteStatus(deleteStatus);
			pmsProductMapper.delete(wrapper);
		}
	}

	// 批量设为新品
	@Override
	public void newStatus(Integer newStatus, List<Integer> ids) {
		for (Integer id : ids) {
			LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
			wrapper.eq(PmsProduct::getId, Long.valueOf(id));
			PmsProduct pmsProduct = new PmsProduct();
			pmsProduct.setNewStatus(newStatus);
			pmsProductMapper.update(pmsProduct, wrapper);
		}

	}

	// 批量上下架商品
	@Override
	public void publishStatus(Integer publishStatus, List<Integer> ids) {
		for (Integer id : ids) {
			LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
			wrapper.eq(PmsProduct::getId, Long.valueOf(id));
			PmsProduct pmsProduct = new PmsProduct();
			pmsProduct.setPublishStatus(publishStatus);
			pmsProductMapper.update(pmsProduct, wrapper);
		}
	}

	// 批量推荐商品
	@Override
	public void recommendStatus(Integer recommendStatus, List<Integer> ids) {
		for (Integer id : ids) {
			LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
			wrapper.in(PmsProduct::getId, Long.valueOf(id));
			PmsProduct pmsProduct = new PmsProduct();
			pmsProduct.setRecommandStatus(recommendStatus);
			pmsProductMapper.update(pmsProduct, wrapper);
		}
	}

	// 批量修改审核状态
	@Override
	public void verifyStatus(String detail, Integer verifyStatus, List<Integer> ids) {
		for (Integer id : ids) {
			LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
			wrapper.in(PmsProduct::getId, Long.valueOf(id));
			PmsProduct pmsProduct = new PmsProduct();
			pmsProduct.setVerifyStatus(verifyStatus);
			pmsProduct.setDetailDesc(detail);
			pmsProductMapper.update(pmsProduct, wrapper);
		}
	}

	// 更新商品
	@Override
	public void updateProduct(Integer id, Map map) {
		LambdaQueryWrapper<PmsMemberPrice> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(PmsMemberPrice::getId, id);
		PmsProduct pmsProduct = (PmsProduct) map.get("pmsProduct");
		pmsProductMapper.updateById(pmsProduct);
		PmsMemberPrice memberPrice = (PmsMemberPrice) map.get("memberPrice");
		if (memberPrice != null) {
			pmsMemberPriceMapper.updateById(memberPrice);
		}
		CmsPrefrenceAreaProductRelation prefrenceAreaProductRelation = (CmsPrefrenceAreaProductRelation) map.get("prefrenceAreaProductRelation");
		if (prefrenceAreaProductRelation != null) {
			cmsPrefrenceAreaProductRelationMapper.updateById(prefrenceAreaProductRelation);
		}
		PmsProductAttributeValue productAttributeValue = (PmsProductAttributeValue) map.get("productAttributeValue");
		if (productAttributeValue != null) {
			pmsProductAttributeValueMapper.updateById(productAttributeValue);
		}
		PmsProductFullReduction productFullReduction = (PmsProductFullReduction) map.get("productFullReduction");
		if (productFullReduction != null) {
			pmsProductFullReductionMapper.updateById(productFullReduction);
		}
		PmsProductLadder productLadder = (PmsProductLadder) map.get("productLadder");
		if (productLadder != null) {
			pmsProductLadderMapper.updateById(productLadder);
		}
		PmsSkuStock skuStock = (PmsSkuStock) map.get("skuStock");
		if (skuStock != null) {
			pmsSkuStockMapper.updateById(skuStock);
		}
		CmsSubjectProductRelation subjectProductRelation = (CmsSubjectProductRelation) map.get("subjectProductRelation");
		if (subjectProductRelation != null) {
			cmsSubjectProductRelationMapper.updateById(subjectProductRelation);
		}
	}

	// 根据商品id获取商品编辑信息
	@Override
	public ProductParamDto queryByProductId(Integer productId) {
		ProductParamDto productParamDto = new ProductParamDto();
		LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(PmsProduct::getId, productId);
		// 获取商品信息
		PmsProduct pmsProduct = pmsProductMapper.selectOne(wrapper);
		BeanUtils.copyProperties(pmsProduct, productParamDto);
		// 查询商品会员价格表
		LambdaQueryWrapper<PmsMemberPrice> priceWrapper = new LambdaQueryWrapper<>();
		priceWrapper.eq(PmsMemberPrice::getProductId, productId);
		List<PmsMemberPrice> pmsMemberPriceList = pmsMemberPriceMapper.selectList(priceWrapper);
		productParamDto.setMemberPriceList(pmsMemberPriceList);
		// 查询优选专区和产品关系表
		LambdaQueryWrapper<CmsPrefrenceAreaProductRelation> relationWrapper = new LambdaQueryWrapper<>();
		relationWrapper.eq(CmsPrefrenceAreaProductRelation::getProductId, productId);
		List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = cmsPrefrenceAreaProductRelationMapper.selectList(relationWrapper);
		productParamDto.setPrefrenceAreaProductRelationList(prefrenceAreaProductRelationList);
		// 查询存储产品参数信息的表
		LambdaQueryWrapper<PmsProductAttributeValue> valueWrapper = new LambdaQueryWrapper<>();
		valueWrapper.eq(PmsProductAttributeValue::getProductId, productId);
		List<PmsProductAttributeValue> valueList = pmsProductAttributeValueMapper.selectList(valueWrapper);
		productParamDto.setProductAttributeValueList(valueList);
		// 查询产品满减表(只针对同商品)
		LambdaQueryWrapper<PmsProductFullReduction> reduceWrapper = new LambdaQueryWrapper<>();
		reduceWrapper.eq(PmsProductFullReduction::getProductId, productId);
		List<PmsProductFullReduction> pmsProductFullReductions = pmsProductFullReductionMapper.selectList(reduceWrapper);
		productParamDto.setProductFullReductionList(pmsProductFullReductions);
		// 查询产品阶梯价格表(只针对同商品)
		LambdaQueryWrapper<PmsProductLadder> ladderWrapper = new LambdaQueryWrapper<>();
		ladderWrapper.eq(PmsProductLadder::getProductId, productId);
		List<PmsProductLadder> productLadderList = pmsProductLadderMapper.selectList(ladderWrapper);
		productParamDto.setProductLadderList(productLadderList);
		// 查询sku的库存
		LambdaQueryWrapper<PmsSkuStock> skuWrapper = new LambdaQueryWrapper<>();
		skuWrapper.eq(PmsSkuStock::getProductId, productId);
		List<PmsSkuStock> pmsSkuStocks = pmsSkuStockMapper.selectList(skuWrapper);
		productParamDto.setSkuStockList(pmsSkuStocks);
		// 查询专题商品关系表
		LambdaQueryWrapper<CmsSubjectProductRelation> subjectWrapper = new LambdaQueryWrapper<>();
		subjectWrapper.eq(CmsSubjectProductRelation::getProductId, productId);
		List<CmsSubjectProductRelation> cmsSubjectProductRelations = cmsSubjectProductRelationMapper.selectList(subjectWrapper);
		productParamDto.setSubjectProductRelationList(cmsSubjectProductRelations);
		return productParamDto;
	}

	@Override
	public Map<Long, PmsProduct> getProductByIds(List<Long> productIds) {
		List<PmsProduct> pmsProducts = pmsProductMapper.selectBatchIds(productIds);
		Map<Long, PmsProduct> productMap = CollUtil.fieldValueMap(pmsProducts, "id");
		return productMap;
	}
}
